<?php
/**
 * Protoweb's models base Class
 *
 * This class provides some base functionality in addittion to CodeIgniter
 * and DMZ. The methods defined here are commonly used in any model.
 *
 * @abstract
 * @author Gonzalez Federico, Viera Leonel
 * @package Protoweb
 * @subpackage Model
 */
abstract class Pw_model extends DataMapper 
{
    public function __construct($id = NULL) 
    {
        parent::__construct($id);
    }

    public function updateManyToManyWithIdArray($relationName,$IdArray=null)
    {
        $class = ucfirst($relationName);

        // saving new and updating existing
        if ( is_array($IdArray) ) {
            $related = new $class();
            foreach($IdArray as $related_id) {
                $related->or_where('id',$related_id);
            }
            $related->get();
            $this->save($related->all);
        }

        // deleting
        foreach($this->$relationName as $related) {
            if ( ! in_array($related->id, $IdArray)) {
                $this->delete($related);
            }
        }
    } // updateManyToManyWithIdArray
}// Class Pw_model

/* End of file pw_model.php */
/* Location: ./application/models/pw_model.php */
